package com.kaifamiao.context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Iterator;

public class InitParamServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获得当前Servlet的初始化参数
        String encoding = this.getInitParameter("charset");
        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        response.setContentType("text/html;charset="+encoding);
        PrintWriter out = response.getWriter();

        final ServletContext application = this.getServletContext();
        // 获得当前 "web application" 的所有初始化参数名称
        Enumeration<String> names = application.getInitParameterNames();
        Iterator<String> itor = names.asIterator();
        while( itor.hasNext() ){
            String name = itor.next();
            // 根据参数名称获得参数值
            String value = application.getInitParameter(name);
            out.printf( "<p>%s : %s</p>\n", name, value );
        }

    }

}
